package master.ghrm.domain;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Transient;

@Entity
public class ChoiceQuestionAnswer extends Answer {
	private static final long serialVersionUID = 1L;

	private static final String[] EXCL_ADD = { "question" };

	private List<QuestionOption> questionAnswers;

	private ChoiceQuestion question;

	/**
	 * Default constructor, for persistence use.
	 */
	public ChoiceQuestionAnswer() {
		super();
	}

	/**
	 * Complete constructor.
	 * 
	 * @param aQuestion question this answer is for.
	 * @param assessment assessment this answer is in.
	 */
	public ChoiceQuestionAnswer(ChoiceQuestion aQuestion, Assessment assessment) {
		super();
		this.question = aQuestion;
		this.setAssessment(assessment);
		this.questionAnswers = new ArrayList<QuestionOption>();
	}

	/**
	 * @return the questionAnswers
	 */
	@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY)
	@JoinTable(name = "tbl_choice_question_answer_question_option", joinColumns = { @JoinColumn(name = "answer_id", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "question_option_id", nullable = false, updatable = false) })
	public List<QuestionOption> getQuestionAnswers() {
		return this.questionAnswers;
	}

	/**
	 * @param questionAnswerList the questionAnswers to set
	 */
	public void setQuestionAnswers(List<QuestionOption> questionAnswerList) {
		this.questionAnswers = questionAnswerList;
	}

	/**
	 * @return the question
	 */
	@Override
	@Transient
	public ChoiceQuestion getQuestion() {
		return this.question;
	}

	/**
	 * @param aQuestion the question to set
	 */
	@Override
	public void setQuestion(Question aQuestion) {
		this.question = (ChoiceQuestion) aQuestion;
	}

	@Override
	public String toString() {
		if (this.questionAnswers.size() == 0) {
			return "<NONE>";
		}
		StringBuilder response = new StringBuilder("{");
		for (QuestionOption option : this.questionAnswers) {
			response.append(option.getText());
			response.append(", ");
		}
		response.delete(response.length() - 2, response.length());
		response.append("}");
		return response.toString();
	}

	@Override
	@Transient
	protected String[] getAdditionalExclAttr() {
		return ChoiceQuestionAnswer.EXCL_ADD;
	}
}
